package cn.tedu.oa.announcement.service.impl;

import cn.tedu.oa.announcement.mapper.AnnouncementMapper;
import cn.tedu.oa.announcement.pojo.dto.AnnouncementQuery;
import cn.tedu.oa.announcement.pojo.dto.AnnouncementSaveParam;
import cn.tedu.oa.announcement.pojo.entity.Announcement;
import cn.tedu.oa.announcement.pojo.vo.AnnouncementVO;
import cn.tedu.oa.announcement.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AnnouncementServiceImpl implements AnnouncementService {
    @Autowired
    AnnouncementMapper announcementMapper;
    @Override
    public List<AnnouncementVO> select(AnnouncementQuery announcementQuery) {
        List<AnnouncementVO> list = announcementMapper.select(announcementQuery);
        return list;
    }

    @Override
    public void save(AnnouncementSaveParam announcementSaveParam) {
        log.info("AnnouncementServiceImpl.save()"+announcementSaveParam);
        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(announcementSaveParam,announcement);
        if (announcement.getId()==null){
            announcement.setCreateTime(new Date());
            announcement.setPublishTime(new Date());
            announcementMapper.insert(announcement);
        }else {
            announcement.setUpdateTime(new Date());
            announcementMapper.update(announcement);
        }

    }

    @Override
    public void delete(Integer id) {
        log.info("AnnouncementServiceImpl.delete()"+id);
        announcementMapper.delete(id);

    }

    @Override
    public Integer selectCount() {
        return announcementMapper.selectCount();
    }
}
